$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'goods/list',
        datatype: "json",
        colModel: [
            {label: '商品id', name: 'id', width: 30},
            {label: '商品类型', name: 'goodsTypeName', index: "goods_type", width: 20},
            {label: '商品名称', name: 'name', width: 20},
            {label: '商品编号', name: 'num', width: 40},
            {
                label: '销售价(单位:元)',
                name: 'sellPrice',
                index: "sell_price",
                width: 20,
                formatter: function (value, options, row) {
                    var total_p = value / 100;
                    var resTotal = accounting.formatMoney(total_p, "", 2);
                    resTotal = resTotal.replace(".00", "");
                    return resTotal;
                }
            },
            {label: '初始数量', name: 'initQty', index: "init_qty", width: 20},
            {label: '剩余数量', name: 'remainQty', index: "init_qty", width: 20},
            {
                label: '状态', name: 'status', width: 20,
                formatter: function (value, options, row) {
                    if (value == 0) {
                        return '<span class="label label-info">下架</span>';
                    } else if (value == 1) {
                        return '<span class="label label-success">上架</span>';
                    } else if (value == 2) {
                        return '<span class="label label-warning">售罄</span>';
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '审核状态', name: 'auditStatus', index: "audit_status", width: 20,
                formatter: function (value, options, row) {
                    if (value == 1) {
                        return '<span class="label label-info">审核中</span>';
                    } else if (value == 2) {
                        return '<span class="label label-success">审核通过</span>';
                    } else if (value == 3) {
                        return '<span class="label label-important">审核不通过</span>';
                    } else {
                        return "";
                    }
                }
            },
            // {
            //     label: '操作', name: 'act', width: 50, formatter: function (value, options, row) {
            //     var id = JSON.parse(row.id);
            //     alert(id);
            //     return '<a href="javascript:void(0)" onclick="vm.updatejq(' + id + ')">修改</a>'
            // }
            // },
        ],
        viewrecords: true,
        height: 720,
        rowNum: 19,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });


});

var vm = new Vue({
    el: '#dcapp',
    data: {
        selected: '',
        q: {
            name: null,
        },
        isAdd: false,
        showList: true,
        title: null,
        goods: {},
        options: [],
        goodsImgList: '[]',
        goodsImg: null,
        formJson: null,
    },
    created: function () {

    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function () {
            vm.isAdd = false;
            vm.getGoodsType();
            var goodsId = getSelectedRow();//选择一条
            if (goodsId == null) {
                return;
            }
            vm.getGoods(goodsId);
            vm.goodsImgList = vm.goods.goodsImg;
            vm.goodsImg = vm.goodsImgList;
            vm.initImg();
            vm.showList = false;
            vm.title = "编辑商品";
        },
        updatejq: function (id) {
            vm.isAdd = false;
            vm.getGoodsType();
            var goodsId = id;
            vm.getGoods(goodsId);
            vm.goodsImgList = vm.goods.goodsImg;
            vm.goodsImg = vm.goodsImgList;
            vm.initImg();
            vm.showList = false;
            vm.title = "编辑商品";
        },
        add: function () {
            vm.goodsImgList = '[]';
            vm.goodsImg = eval('(' + vm.goodsImgList + ')');
            vm.initImg();
            vm.isAdd = true;
            vm.getGoodsType();
            vm.goods = {};
            vm.showList = false;
            vm.title = "增加商品";
        },
        saveOrUpdate: function () {
            var url = vm.goods.id == null ? "goods/saveGoods" : "goods/updateGoods";
            vm.goods.sellPrice = vm.goods.sellPrice * 100;
            vm.goods.goodsImg = vm.goodsImg;
            vm.formJson = JSON.stringify(vm.goods);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: vm.formJson,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function () {
            var goodsIds = getSelectedRows();
            if (goodsIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "goods/delete",
                    contentType: "application/json",
                    data: JSON.stringify(goodsIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getGoods: function (goodsId) {
            $.ajax({
                type: "GET",
                url: baseURL + "goods/info/" + goodsId,
                async: false,
                success: function (r) {
                    vm.goods = r.goods;
                    vm.goods.sellPrice = vm.goods.sellPrice / 100;
                }
            });
        },
        getGoodsType: function () {
            $.get(baseURL + "goods/goodsTypeList", function (r) {
                vm.options = r.goodsTypes;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },
        initImg: function () {
            $("#upload_store").initWebUploader(vm.goodsImg, "goodsImg", 6, vm.goods.id, vm.goodsImg.length);
        },
        up: function () {
            var goodsIds = getSelectedRows();
            if (goodsIds == null) {
                return;
            }

            confirm('确定上架选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "goods/up",
                    contentType: "application/json",
                    data: JSON.stringify(goodsIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        down: function () {
            var goodsIds = getSelectedRows();
            if (goodsIds == null) {
                return;
            }
            confirm('确定下架选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "goods/down",
                    contentType: "application/json",
                    data: JSON.stringify(goodsIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },


    }
});







